﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using LeoShi.Soft.OpenSinaAPI;
using System.IO;
using System.Xml;
using LTP.Common;
using WeiBo.SinaAPI;

namespace TCS.Web.MembersHome
{
    public partial class findfollowers : System.Web.UI.Page
    {
        protected string total_count;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                ViewState["_searchkey"] = "";

                BindDropDown();
                //BindData();
            }
        }
        void BindDropDown()
        {
            DataSet dsprovinces = new TCS.BLL.Weibo.WeiBo_provinces().GetProvinces();
            PublicDropdownList.BindDropDownListData(dropProvinces, dsprovinces, "Name", "id");
        }
        void BindData()
        {
            var httpRequest = HttpRequestFactory.CreateHttpRequest(Method.GET);
            httpRequest.Token = Session["oauth_token"].ToString();
            httpRequest.TokenSecret = Session["oauth_token_secret"].ToString();
            httpRequest.UserRemoteIP = Request.UserHostAddress;
            string url = "http://api.t.sina.com.cn/statuses/search.json?";
            url += ViewState["_searchkey"].ToString();
            string ret = httpRequest.Request(url,string.Empty);

            System.Globalization.CultureInfo provider = System.Globalization.CultureInfo.InvariantCulture;
          
            List<LTP.Common.Status> listObj = JSONConvert.DeserializeJsonToListObject(ret);
            List<LTP.Common.Status> listObj2 = new List<Status>();
            //PagedDataSource pds = new PagedDataSource();
            //pds.DataSource = listObj;
            //pds.AllowPaging = true;
            //pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
            //pds.PageSize = AspNetPager1.PageSize;
            //AspNetPager1.RecordCount = listObj.Count;
            //pds.PageSize = 10;
          
            this.rptList.DataSource = listObj;
            this.rptList.DataBind();
             
        }
        protected void dropProvinces_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (dropProvinces.SelectedValue != "")
            {
                DataSet dsprovinces = new TCS.BLL.Weibo.WeiBo_provinces().GetCityByPID(int.Parse(dropProvinces.SelectedValue));
                PublicDropdownList.BindDropDownListData(dropCity, dsprovinces, "Name", "id");
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.lbtnNext.ToolTip = "1";
            if (txtKeys.Text.Trim()!= "")
            {
                ViewState["_searchkey"] = "count=10&page=" + lbtnNext.ToolTip + "&q=" + txtKeys.Text.Trim();
            }
            if (rbtAll.Checked)
            {
                ViewState["_searchkey"] += "&filter_ori=0";
            }
            else if (rbtOriginal.Checked)
            {
                ViewState["_searchkey"] += "&filter_ori=5";
            }
            else
            {
                ViewState["_searchkey"] += "&filter_ori=4";
            }
            if (dropProvinces.SelectedValue != "")
            {
                ViewState["_searchkey"] += "&province="+dropProvinces.SelectedValue;
            }
            if (dropCity.SelectedValue != "")
            {
                ViewState["_searchkey"] += "&city="+dropCity.SelectedValue;
            }
            ViewState["_searchkey"] += "&sort=1&page=1&count=40";
        //  http://api.t.sina.com.cn/statuses/search.json?q=api&sort=1&page=1&count=10
            BindData();
        }

        protected void lbtnClearSearchKey_Click(object sender, EventArgs e)
        {
            ViewState["_searchkey"] = "";
            BindData();
        }

        protected void lbtnNext_Click(object sender, EventArgs e)
        {
            lbtnNext.ToolTip = Convert.ToString(Convert.ToInt32(lbtnNext.ToolTip) + 1);
            BindData();
        }
        protected void AspNetPager1_PageChanged(object src, EventArgs e)
        {
            BindData();
        }
        protected string GetCount(string wbid)
        {
            var httpRequest = HttpRequestFactory.CreateHttpRequest(Method.GET);
            httpRequest.Token = Session["oauth_token"].ToString();
            httpRequest.TokenSecret = Session["oauth_token_secret"].ToString();
            httpRequest.UserRemoteIP = Request.UserHostAddress;
            string url = "http://api.t.sina.com.cn/statuses/counts.json?ids=" + wbid;
            try
            {
                string ret = httpRequest.Request(url, string.Empty);

               List<LTP.Common.StatusCount> listObj = JSONConvert.DeserializeJsonToListObjectStatusCount(ret);
               return "评论数：" + listObj[0].comments + "  转发数：" + listObj[0].rt;
            }
            catch (Exception ex)
            {

                return "未知";
            }
        }
        protected string StatusFormat(string text)
        {
            DataSet dstemp = new DataSet();
            DataSet ds = (DataSet)Cache.Get("ds_emotion_cache");
            if (ds != null)
            {
                dstemp = ds;
            }
            else
            {
                dstemp = new TCS.BLL.Emotion().GetList("");
                Cache.Insert("ds_emotion_cache", dstemp, null, DateTime.Now.AddHours(1), TimeSpan.Zero);
            }

            for (int i = 0; i < dstemp.Tables[0].Rows.Count; i++)
            {
                text = text.Replace(dstemp.Tables[0].Rows[i]["phrase"].ToString(), "<img src=\"" + dstemp.Tables[0].Rows[i]["url"].ToString() + "\" alt=\"" + dstemp.Tables[0].Rows[i]["phrase"].ToString().Replace("[","").Replace("]","") + "\" >");
            }
            return text;
        }
        /// <summary>
        /// 新浪时间转换
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public string SinaDateConvert(object obj)
        {
            string result = DateTime.Now.ToString();
            result = StringHelper.SinaDataConvert(obj.ToString());
            return result;
        }
        /// <summary>
        /// 数据处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void rptList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {

            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
              
            }
        }
    }
}
